define(function(require) {

    require('../../../c/base/index');

    require('../../../c/nav/index');
    require('./taobaoOrderMonitor-tpl');
    window.moment = require('./moment')

    var taobaoOrderMonitorTpl = juicer.templates['taobaoOrderMonitor.juicer.html'];
    var elContent = $('.taobao-order-search-list');


    juicer.register('timeFormat', function(time){
    	return moment(time).format('YYYY-MM-DD HH:mm:ss')
    });



    juicer.register('leftTimeFormat', function(time,timestamp){
    	var left = time + (15 * 60 * 1000) - timestamp;
    	return left ;//moment(time).format('YYYY-MM-DD HH:mm:ss')
    });


    var leftTimeRoll ;

    var queryRoll ;

    var elCity = $('#J_City');
    var elPlatform =$('#J_Platform');


    var Monitor = {
        init: function() {
            //初始化菜单选中效果
            $(".fa-circle-o").each(function() {
                if ($(this).parents("a").text() == "淘宝异常订单") {
                    $(this).parents("a").parents("li").addClass("active");
                }
            });
            this.bind()
        },
        bind:function(){
        	var self = this;
        	var btn = $('#J_Search');
        	btn.on('click',function(){

        		if(queryRoll)clearInterval(queryRoll);
        		Monitor.queryData();
        		queryRoll = setInterval(function(){
        			Monitor.queryData();
        		},60 * 1000);
        		
        	});
        },
        queryData:function(){
        	var self = this;
        	$.ajax({
                "type": 'POST',
                "url": '/trade/crm/order/queryAcceptWarn',
                "dataType": "json",
                contentType: "application/json",
                xhrFields: {
                    withCredentials: true
                },
                "data": JSON.stringify({
                	onlinePayType:elPlatform.val(),
                	cityCode:elCity.val()
                }),
                contentType: "application/json",
                "success": function (req) {
                	Monitor.callback(req)

                },
                error: function (e) {
                    //请求出错处理
                    alert('接口异常')
                }
            });
        },
        callback:function(req){
            var len = req.entry.length;
            var timestamp = req.timestamp;

			for(var i=0;i<len;i++){
        		var data = req.entry[i];
        		data.leftTime = data.buyer2uspayTime + (15 * 60 * 1000) - timestamp;
        		data.leftTimeFormat = moment(data.leftTime).format('mm:ss');
        	}
        	var html = taobaoOrderMonitorTpl({data:req});

        	if(!len){
        		html = '<li><center>没有记录</center></li>';
        	}
            elContent.html(html);





            if(leftTimeRoll)clearInterval(leftTimeRoll);
           	leftTimeRoll = setInterval(function(){
           		for(var i=0;i<len;i++){
	        		var data = req.entry[i];
	        		data.leftTime = data.leftTime-1000;
	        		data.leftTimeFormat = moment(data.leftTime).format('mm:ss');
	        		if(data.leftTime <=0){
	        			data.leftTimeFormat = '0';
	        			$('.J_LeftTimeFormat[data-id="'+data.bizOrderId+'"]').parent().html('已经拒单')
	        			return;
	        		}
	        		$('.J_LeftTimeFormat[data-id="'+data.bizOrderId+'"]').html(data.leftTimeFormat + ' ');
	        	}
           	},1000);



        }
    };


    Monitor.init();
});




// // <!-- 


// a = {
//   "entry": [
//     {
//       "acceptTime": null,
//       "activityCity": null,
//       "addressSnapshot": "五常街道文一西路969号6号楼",
//       "area": null,
//       "attributes": "{\"xiaoErMobile\":13222222222,\"cityName\":\"杭州\",\"reduceType\":3,\"xiaoErId\":929564,\"deductFreightCharge\":0,\"outWayShopId\":\"1000330255\",\"managerMobile\":13646834799,\"freightCharge\":300,\"shopCity\":\"杭州\",\"outDeliveryPrice\":\"300\",\"rejectTime\":\"2016-07-11 20:29:24\",\"rejectUser\":126405523,\"userPayFreightCharge\":300,\"outOriginPrice\":\"3922\",\"outWayId\":\"194323470153005\",\"outTotalPrice\":\"2411\",\"actReduceIds\":[\"10030\"],\"isPreSale\":false,\"oldStatus\":\"2\",\"orderMobile\":13127525239,\"shopType\":1,\"xiaoErNick\":\"小二对接\",\"shopName\":\"手淘对接测试店2\",\"managerId\":929468,\"outWay\":\"10\",\"managerNick\":\"pengxin\",\"itemKinds\":2,\"sellerMobile\":19905718888}",
//       "bizOrderId": 14484195531,
//       "buyAmount": 2,
//       "buyer2uspayId": 9905,
//       "buyer2uspayTime": 1469602801000,
//       "buyerCity": null,
//       "buyerId": 126405531,
//       "buyerNick": "苏穆",
//       "buyerOrderFunds": [],
//       "canModifyAddress": 0,
//       "cancelTime": null,
//       "cancelUser": null,
//       "cancelledBy": 12,
//       "cardList": null,
//       "cityCode": 330100,
//       "cityName": "杭州",
//       "comment": null,
//       "commissionFee": 0,
//       "community": "",
//       "communityId": null,
//       "confirmDeliveryTime": null,
//       "confirmedBySystem": null,
//       "customerReminderMark": null,
//       "dailySequence": 14,
//       "deductFreightCharge": null,
//       "deliveryCode": 6664,
//       "deliveryDeduct": 0,
//       "deliveryLogisticsnos": null,
//       "deliveryType": 0,
//       "deliveryerMobile": null,
//       "deliveryerName": null,
//       "evalCount": null,
//       "evalExpire": null,
//       "ext": {},
//       "feature": null,
//       "freightCharge": null,
//       "freightChargeFakeReduce": null,
//       "gmtCreate": 1469602800000,
//       "groupCurCount": null,
//       "groupFullTime": null,
//       "groupGmtCreate": null,
//       "groupId": null,
//       "groupMin": null,
//       "groupPeopleCount": null,
//       "groupPlanOverTime": null,
//       "groupShareAbstract": null,
//       "groupShareTitle": null,
//       "groupStatus": null,
//       "isAccept": 0,
//       "isCanCancelOrder": true,
//       "isDeleted": 0,
//       "isDownloadOrderForLogistics": false,
//       "isOfficialPreSale": false,
//       "isPreSale": false,
//       "isResendOrder": false,
//       "isTryCity": null,
//       "itemId": 100030603,
//       "itemName": "dumiao兑换券测试,dumiao2测试入库到其他仓库",
//       "itemSumPrice": 0,
//       "landmarkId": 0,
//       "lastOpUserId": 126405523,
//       "lastOpUserName": "蒋皓",
//       "lat": 30.286184,
//       "lng": 120.034043,
//       "logisticsCompanyCodeMap": {},
//       "logisticsCompanyName": null,
//       "logisticsDOList": [],
//       "logisticsNo": null,
//       "logisticsStatus": 0,
//       "managerMobile": 13646834799,
//       "managerNick": "pengxin",
//       "marketActivityId": null,
//       "mobile": 13127525239,
//       "notArriveOnTime": 0,
//       "notCountSmallCharge": 0,
//       "notSendGoodOnTime": 0,
//       "notSendOrderReason": null,
//       "onlinePayType": 12,
//       "openid": null,
//       "orderEval": null,
//       "orderFunds": null,
//       "orderMobile": null,
//       "orderNick": null,
//       "orderType": 1,
//       "originalPrice": 1400,
//       "outOrderId": "201607112026031264055311002483",
//       "payStatus": 2,
//       "payTime": null,
//       "payType": 2,
//       "platformCode": null,
//       "platformName": null,
//       "preSalePullBackTime": null,
//       "preSaleSendEndTime": null,
//       "preSaleSendStartTime": null,
//       "promotionPrice": 1301,
//       "property": null,
//       "province": null,
//       "psf": 0,
//       "reduce": 99,
//       "reduceId": 0,
//       "refundCount": 0,
//       "refundInfo": null,
//       "refundReceived": false,
//       "refundStatus": 20,
//       "refundStatusStr": null,
//       "refundSuccessDatetime": null,
//       "rejectReason": "缺货／断货",
//       "rejectValue": 2,
//       "reminderMarkCount": null,
//       "scFreightCharge": null,
//       "sellerBenefit": 0,
//       "sellerCouponSubsidy": 0,
//       "sellerFullReduceSubsidy": 0,
//       "sellerMobile": null,
//       "sellerNick": null,
//       "sellerOrderFunds": [],
//       "sellerPackageSubsidy": 0,
//       "sellerPsfSubsidy": 0,
//       "sellerSpecialSubsidy": 0,
//       "sendLogisticsnos": null,
//       "sendTime": null,
//       "shopCouponId": 0,
//       "shopCouponPrice": 0,
//       "shopId": 1002483,
//       "shopName": "手淘对接测试店2",
//       "shopType": 1,
//       "status": 2,
//       "subOrders": [
//         {
//           "bizOrderId": 14472285531,
//           "buyAmount": 1,
//           "combinationItemInfo": [],
//           "communityId": null,
//           "gmtCreate": 1468239963000,
//           "isCombinationItem": null,
//           "isDeleted": 0,
//           "itemId": 100030603,
//           "itemName": "dumiao兑换券测试",
//           "itemOriginPrice": null,
//           "itemPicUrl": "n/07/01/1467339024974_9946.jpg",
//           "itemPrice": 100,
//           "mobile": "13127525239",
//           "parentOrderId": 14484195531,
//           "property": "0.01",
//           "shopId": 1002483,
//           "skuId": null,
//           "totalPrice": 100,
//           "type": 1,
//           "unit": ""
//         },
//         {
//           "bizOrderId": 14462295531,
//           "buyAmount": 1,
//           "combinationItemInfo": [],
//           "communityId": null,
//           "gmtCreate": 1468239963000,
//           "isCombinationItem": null,
//           "isDeleted": 0,
//           "itemId": 100030601,
//           "itemName": "[独妙测试]dumiao2测试入库到其他仓库",
//           "itemOriginPrice": null,
//           "itemPicUrl": "n/07/05/1467703421256_6019.jpg",
//           "itemPrice": 1000,
//           "mobile": "13127525239",
//           "parentOrderId": 14484195531,
//           "property": "1个，约1.5-2斤",
//           "shopId": 1002483,
//           "skuId": null,
//           "totalPrice": 1000,
//           "type": 1,
//           "unit": "个"
//         }
//       ],
//       "sysDate": 1469604633993,
//       "timeline": [],
//       "totalPrice": 1301,
//       "tradeSuccessDatetime": null,
//       "unit": null,
//       "us2SellerPayBlockedType": null,
//       "us2sellerpayId": null,
//       "us2sellerpayStatus": 0,
//       "us2sellerpayTime": null,
//       "validTime": 1469602801000,
//       "xiaoErId": 929564,
//       "xiaoErMobile": 13222222222,
//       "xiaoErNick": "小二对接"
//     }
//   ],
//   "message": "处理成功！",
//   "responseCode": 1,
//   "status": true,
//   "timestamp": 1469604633991
// } 